class SlaInstanceController < ApplicationController

  def create
    begin
      CloudController.logger.info("come in  create")
      @name = body_params[:name]
      @instances = body_params[:instances]
      @memsize = body_params[:resources][:memory]
      @disksize = body_params[:resources][:disk]
      @service = body_params[:services]
      @framework=body_params[:staging][:framework]
      @runtime=body_params[:staging][:runtime]
      #@app_starttime = Time.at(body_params[:app_starttime]).strftime("%m/%d/%Y - %I:%M%p")
      #@app_stoptime =Time.at(body_params[:app_stoptime]).strftime("%m/%d/%Y - %I:%M%p")
      #@app_starttime = DateTime.parse(body_params[:app_starttime]).to_i
      #@app_stoptime =DateTime.parse(body_params[:app_stoptime]).to_i
      @app_starttime = body_params[:app_starttime]
      @app_stoptime =body_params[:app_stoptime]
      @price =body_params[:price]
      @fine =body_params[:fine]
      @pricing_mode =body_params[:pricing_mode]
      CloudController.logger.info("@app_starttime:#{@app_starttime}")
      CloudController.logger.info("@app_stoptime:#{@app_stoptime}")

      sla_instance = ::SlaInstance.new(:name =>@name , :instances => @instances,:memsize=>@memsize,:disksize=>@disksize,:framework=>@framework,:service=>@service,
                                       :runtime =>@runtime, :app_starttime=>@app_starttime, :app_stoptime=>@app_stoptime,:price=>@price,:fine=>@fine,:pricing_mode=>@pricing_mode )
      CloudController.logger.info("sla_instance :#{sla_instance.to_json}")
      if sla_instance.save
        render :json => {}
      else
        raise CloudError.new(CloudError::BAD_REQUEST)
      end
    end

  end

  def list

    CloudController.logger.info("come in cloudcontroller list")
    slainstance_list = ::SlaInstance.all.map do |sla_instance|
      slainstance_hash = {:name =>sla_instance.name , :instances => sla_instance.instances,:memsize=>sla_instance.memsize,:disksize=>sla_instance.disksize,:framework=>sla_instance.framework,:service=>sla_instance.service,
                          :runtime =>sla_instance.runtime, :app_starttime=>sla_instance.app_starttime, :app_stoptime=>sla_instance.app_stoptime,
                          :created_at=>sla_instance.created_at,:updated_at=>sla_instance.updated_at,:price=>sla_instance.price,:fine=>sla_instance.fine,:pricing_mode=>sla_instance.pricing_mode}
      slainstance_hash
    end
    CloudController.logger.info("slainstance_list :#{slainstance_list}")
    render :json => slainstance_list
  end




  def delete

    @sla_instance = ::SlaInstance.find_by_name(params['name'])
    CloudController.logger.info("delete @sla_instance=#{@sla_instance.as_json}")
    @sla_instance.destroy
    #render :json => {}
    render :status => 204, :nothing => true
  end

  def info
    @sla_instance = ::SlaInstance.find_by_name(params['name'])
    CloudController.logger.info("@sla_instance=#{@sla_instance}")
    if @sla_instance
      slainstance_info={:name =>@sla_instance.name , :instances => @sla_instance.instances,:memsize=>@sla_instance.memsize,:disksize=>@sla_instance.disksize,:framework=>@sla_instance.framework,:service=>@sla_instance.service,
      :runtime =>@sla_instance.runtime, :app_starttime=>@sla_instance.app_starttime, :app_stoptime=>@sla_instance.app_stoptime,
      :created_at=>@sla_instance.created_at,:updated_at=>@sla_instance.updated_at,:price=>@sla_instance.price,:fine=>@sla_instance.fine,:pricing_mode=>@sla_instance.pricing_mode}

      CloudController.logger.info("slainstance_info=#{slainstance_info}")
      render :json => slainstance_info

    else
      raise CloudError.new(CloudError::SLA_NOT_FOUND)
    end


  end




end
